<?php
namespace CC\Service;
use Bisna\Service\Service as BisnaService;

class ContactService extends BisnaService
{
    /**
     * Create a new contact.
     * 
     * @param array $contactDetails An array containing the contacts details
     * @param integer $assignToId The ID to assign the contact to. (User by default)
     * @param boolean $isProviderContact If true, then the contact will be assigned 
     * to the provider
     * @return boolean Return true on success
     */
    public function addContact($contactDetails, $assignToId, $isProviderContact = false)
    {
        $em = $this->getEntityManager();
        $contactEntity = new \CC\Entity\Contact();
        
        if(is_array($contactDetails))
        {
            //if(!(isset($contactDetails['name']) || (isset($contactDetails['emailAddress']))))
            //    return false;
        
            // Iterate through contact details array
            foreach($contactDetails as $field => $value)
            {
                // Check if the mutator exists 
                $mutator = 'set'.ucfirst($field);
                if(method_exists($contactEntity, $mutator))
                {
                    // Set mutator property
                    $contactEntity->{$mutator}($value);
                }
            }
            
            // Assign to user or provider
            if($isProviderContact)
            {
                $provider = $em->getReference('CC\Entity\Provider', $assignToId);
                $contactEntity->setProvider($provider);
            }
            else
            {
                $user = $em->getReference('CC\Entity\User', $assignToId);
                $contactEntity->setUser($user);
            }
        
            try 
            {
                $em->persist($contactEntity);
                $em->flush();
                
                return true;
            }
            catch(Exception $e) { }
        }
        
        return false; 
    }

    /**
     * Search for a contact that is assigned to the user or provider.
     * 
     * @param string $query String to search for
     * @param integer $userId User Id
     * @return array
     */
    public function findContact($query, $userId)
    {
        $em = $this->getEntityManager();
        $user = $em->getRepository('CC\Entity\User')->findOneById($userId);
        
        $contacts = array();
        $providerId = $user->getProvider()->getId();
        
        $dql = 'SELECT c 
        		FROM CC\Entity\Contact c
        		LEFT JOIN c.user u
        		LEFT JOIN c.provider p
        		WHERE (c.name LIKE ?1 OR c.emailAddress LIKE ?1)
        		  AND (u.id = ?2 OR p.id = ?3)';
        
        $contactEntities = $this->getEntityManager()->createQuery($dql)
                                                    ->setParameter(1, '%'.$query.'%')
                                                    ->setParameter(2, $userId)
                                                    ->setParameter(3, $providerId)
                                                    ->getResult();
        
        foreach($contactEntities as $contact)
        {
            $tmp = array(
                'id'    => $contact->getId(),
                'name'  => $contact->getName(),
                'email' => $contact->getEmailAddress()
            );
            
            $contacts[] = $tmp;
        }
        
        return $contacts;
    }
    
    public function findUser($query)
    {
        
    } 
}
?>